package com.bestcem.xm.deliver.controller;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.common.core.exception.BusinessException;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.deliver.controller.v2.param.CreateEmbedParam;
import com.bestcem.xm.deliver.controller.v2.param.EditEmbedParam;
import com.bestcem.xm.deliver.controller.v2.param.ReleaseEmbedParam;
import com.bestcem.xm.deliver.entity.DeliverEmbedDeliver;
import com.bestcem.xm.deliver.service.DeliverEmbedDeliverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.bestcem.xm.deliver.enums.EmbedDeliverCTypeEnum.EMBEDDELIVER_C_TYPE_MOBILE;
import static com.bestcem.xm.deliver.enums.EmbedDeliverCTypeEnum.EMBEDDELIVER_C_TYPE_PC;


@Slf4j
@Api(tags = "问卷投放-网站嵌入")
@RestController
@RequiredArgsConstructor
@RequestMapping("/deliver")
public class DeliverEmbedController {

    private @NonNull DeliverEmbedDeliverService deliverEmbedDeliverService;

    @ApiOperation(value = "创建网页内嵌链接")
    @PostMapping("/embed_deliver")
    public XmResultJson<JSONObject> createEmbedDeliver(@RequestBody CreateEmbedParam param) {
        param.setUserId(SecurityContextHolder.getUserId());
        param.setOrgId(SecurityContextHolder.getOrgId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", deliverEmbedDeliverService.createEmbedDeliver(param));
        return XmResultJson.success(jsonObject);
    }

    @ApiOperation(value = "获取网页内嵌链接")
    @GetMapping("/embeddeliver/select")
    public XmResultJson<JSONObject> selectEmbedDeliver(@RequestParam(name = "deliver_id") String deliverId) {
        JSONObject reJson = new JSONObject();
        DeliverEmbedDeliver deliver = deliverEmbedDeliverService.getEmbedDeliver(deliverId);
        reJson.put("embed_deliver", deliver);
        return XmResultJson.success(reJson);
    }

    @ApiOperation(value = "编辑网页内嵌链接")
    @PutMapping("/embeddeliver/edit/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<JSONObject> editEmbedDeliver(@PathVariable String id, @Validated @RequestBody EditEmbedParam param) {
        param.setUserId(SecurityContextHolder.getUserId());
        param.setOrgId(SecurityContextHolder.getOrgId());
        JSONObject jsonObject = deliverEmbedDeliverService.editEmbedDeliver(id, param);
        return XmResultJson.success(jsonObject);
    }

    @ApiOperation(value = "发布指定c_type类型的嵌入投放数据")
    @PostMapping("/embeddeliver/{id:[a-fA-F0-9]{24}}/release")
    public JSONObject releaseEmbedDeliver(@PathVariable String id, @Validated @RequestBody ReleaseEmbedParam param) {
        if (param.getCType() != EMBEDDELIVER_C_TYPE_PC.getValue() && param.getCType() != EMBEDDELIVER_C_TYPE_MOBILE.getValue()) {
            throw new BusinessException("c_type is not found");
        }

        deliverEmbedDeliverService.releaseEmbedDeliver(id, param.getCType());

        JSONObject reJson = new JSONObject();
        reJson.put("status", true);
        return reJson;
    }
}
